import { reqregister } from "../../http/api"
import { Toast } from "antd-mobile"

/* 1.设置初始状态 */
const initState = {
    user:{
        phone:"",
        nickname:"",
        password:"",
      }
}


/* 改名 */
const types = {
    CHANGE_USER:"REGISTER_CHANGE_USER",
}

/* reduce 修改state */
const reducer = (state = initState,action)=>{
    switch(action.type){
        case types.CHANGE_USER:
            return{
                ...state,
                user:{
                    ...state.user,
                    [action.key]:action.value
                }
            }
            default:
            return state;
    }
}

/* 4.主要逻辑 */
export const actions = {
    changeUser:(value,key)=>({type:types.CHANGE_USER,key,value}),
    reqregister:(push)=>(dispatch,getState)=>{
        let user = getUser(getState())
        reqregister(user).then(res=>{
            if(res.data.code === 200){
                Toast.show({
                    content:res.data.msg,
                })
                push("/login")
            }
        })
    }
}


/* 5.导出 */
export const getUser = state => state.Register.user;

export default reducer;